﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Jage.Win32;

namespace Jage.GameEditor.Controls.Sprites
{
    public partial class SpriteBrowser : Form
    {

        private ListViewDatabinder<Data.Spritesheet> _spritesheetDatabinder;
        private ListViewDatabinder<Data.Sprite> _spriteDatabinder;

        public SpriteBrowser()
        {
            InitializeComponent();
            _spritesheetDatabinder = new ListViewDatabinder<Data.Spritesheet>(listViewSpritesheets, "SpritesheetId");
            _spritesheetDatabinder.AddColumnDefinition("Name");
            _spriteDatabinder = new ListViewDatabinder<Data.Sprite>(listViewSprites, "SpriteId");
            _spriteDatabinder.AddColumnDefinition("SortIndex");
        }

        private void SpriteBrowser_Load(object sender, EventArgs e)
        {
            RefreshSpritesheets();
            RefreshSprites();
        }

        private void RefreshSprites()
        {
            if (listViewSpritesheets.SelectedItems.Count > 0)
            {
                var spritesheetId = _spritesheetDatabinder.SelectedIds.First();
                var sprites =
                    Data.JageDb.Default.Sprites.Where(s => s.SpritesheetId == spritesheetId);
                if (reverse)
                {
                    sprites = from s in sprites orderby s.SortIndex descending select s;
                }
                else
                {
                    sprites = from s in sprites orderby s.SortIndex ascending select s;
                }
                _spriteDatabinder.Databind(sprites);
                spritesheetPreview1.SetSpritesheet(_spritesheetDatabinder.SelectedObjects.First());
            }
        }

        private void RefreshSpritesheets()
        {
            _spritesheetDatabinder.Databind(Data.JageDb.Default.Spritesheets.OrderBy(s => s.Name));
        }

        public IEnumerable<Data.Sprite> SelectedSprites
        {
            get
            {
                return _spriteDatabinder.SelectedObjects;
            }
        }

        private void listViewSpritesheets_SelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshSprites();
        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _spriteDatabinder.SelectAll();
        }
        
        private void listViewSprites_Click(object sender, EventArgs e)
        {
            if (listViewSprites.SelectedItems.Count > 0)
            {
                spritesheetPreview1.SelectedSpriteIds = _spriteDatabinder.SelectedIds.ToList();
                spritesheetPreview1.Refresh();
            }
        }

        private bool reverse = false;

        private void listViewSprites_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            reverse = !reverse;
            RefreshSprites();
        }

    }
}


